﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--   Matches C# Version as of 11/17/08  Reggie Bradshaw--!>
<head>
<title>CodeSmith Linq to Sql Templates</title>
<style type="text/css">
.style1 {
	color: #FF0000;
}
</style>
</head>

<body>

<h1>Table of Contents</h1>
<ul>
	<li><a href="#Overview">Overview</a></li>
	<li><a href="#Features">Features</a></li>
	<li><a href="#QuickStart">Quick Start</a></li>
	<li><a href="#Dbml.cst">Dbml.cst Template</a></li>
	<li><a href="#Entities.cst">Entities.cst Template</a></li>
	<li><a href="#Managers.cst">Managers.cst Template</a></li>
	<li><a href="#Queries.cst_Template">Queries.cst Template</a></li>
	<li><a href="#KnownIssues">Known Issues</a></li>
	<li><a href="#Roadmap">Roadmap</a></li>
	<li><a href="#Download">Download</a></li>
</ul>
<h2><a title="Overview" name="Overview"></a>Overview</h2>
<p>PLINQO, which stands for Professional LINQ to Objects, is a collection of CodeSmith 
templates that are meant to replace and extend the LINQ to SQL designers that are 
included with Visual Studio 2008.</p>
<h2><a title="Features" name="Features">Features</a></h2>
<p>The templates have the following features.</p>
<ul>
	<li>Generate or update a LINQ to SQL dbml file from a database schema.<ul>
		<li>Includes all tables, stored procedures, functions, and views with the 
		ability to exclude objects based on regex patterns.</li>
		<li>Ability to automatically remove object prefix and suffixes (ie. tbl_ 
		and usp_).</li>
		<li>Dbml file can still be customized with the normal Visual Studio 2008 
		designer.</li>
		<li>Dbml file can be refreshed based on the current database schema without 
		losing customizations. <em>(See Safe Attributes)</em></li>
	</ul>
	</li>
	<li>Generation of the LINQ to SQL DataContext class.</li>
	<li>Generation of the LINQ to SQL entity classes.<ul>
		<li>Generates one file per entity instead of one massive file.</li>
		<li>Generates partial classes where custom code can be written and won&#39;t 
		be overwritten.</li>
                <li>Gebnerates Extension Modules with CodeSmith Merge capability so that sections of 
                    code can be written and not overwrtitten. </li>
		<li>Generated entity files are added to the project as code behind files 
		to their corresponding custom entity files.</li>
	</ul>
	</li>
	<li>Generation of entity manager classes.<ul>
		<li>Adds customizable business rules engine to enforce entity validation, 
		business and security rules.</li>
		<li>Provides access to common queries based on primary keys, foreign keys, 
		and indexes.</li>
		<li>Common queries are exposed as IQueryable so they can be extended.</li>
	</ul>
	</li>
	<li>Generation of query extension methods.</li>
	<li>All templates can be customized to meet your needs.</li>
</ul>
<h2>Advantages Over Designer</h2>
<p><font class="messagecontent">There are several advantages to using the PLINQO 
templates over the Visual Studio designer. Here is a list of the main reasons:</font></p>
<ol>
	<li><font class="messagecontent">Remove the designer black box and allow for 
	customization of the output while still retaining the ability to use the .dbml 
	designer to make customizations inside of Visual Studio.</font></li>
	<li><font class="messagecontent">Ability to easily generate your entire .dbml 
	file for a database and then the ability to regenerate that .dbml file as 
	the schema changes. The regeneration preserves any customizations you may 
	have made such as entity, property and relationship names. With the 
	designer, if you make a database change, you need to drop the entity and 
	re-add it to get any new columns or data type changes, which would cause you 
	to lose any customizations you may have made. Also, using the templates 
	allows you to exclude unwanted tables, stored procedures and views using 
	filter expressions and automatically strip / clean entity and property names 
	of things like prefixes and suffixes that your database schema may be using 
	(ie. tbl_Customer to Customer).</font></li>
	<li><font class="messagecontent">A business rules engine that allows you to 
	enforce things like property length rules, required field rules, regex data 
	validation rules as well as several other built in rules including 
	authorization rules. The SubmitChanges method on the data context object 
	will automatically run the rules against any entities in your change set. If 
	all rules are not met, a BrokenRulesException will be thrown that contains a 
	list of the broken rules.</font></li>
	<li><font class="messagecontent">A manager class is generated for each 
	entity that encapsulates all actions taken on an entity. Known common 
	actions like retrieving entities by primary key, indexes, and foreign keys 
	are generated. Any custom actions can be added and will be preserved during 
	regeneration. While LINQ makes it easy to sprinkle your data access logic 
	throughout your entire application, we still believe its poor design to do 
	so and that is why we have included the manager classes. </font></li>
</ol>
<h2><a title="QuickStart" name="QuickStart">Quick Start</a></h2>
<p>Use the following steps to get started using the Linq to Sql templates.</p>
<ol>
	<li>Create a new Class Library project in Visual Studio 2008.</li>
	<li>Add a new CodeSmith project file to the Visual Studio project.<br />
	</li>
	<li>Add a new Output to the project file for the
	<a href="#Dbml.cst">Dbml.cst</a> template.<br />
	</li>
	<li>Add another Output to the project file for the
	<a href="#Entities.cst">Entities.cst</a> template.<br />
	</li>
	<li>Optionally, Add an Output for the
	<a href="#Managers.cst">Managers.cst</a> template.</li>
    <li>Optionally, Add an Output for the 
    <a href="#Queries.cst">Queries.cst</a> template<br />
	</li>
	<li>Set the <i>Sample.csp -&gt; Output Options -&gt; Add Outputs</i> to Project to 
	unchecked.&nbsp; The templates update the project for you.</li>
	<li>Finally, Generate the Outputs. (Figure 5)<br />
	</li>
</ol>
<h2><a title="Dbml.cst" name="Dbml.cst">Dbml.cst Template</a></h2>
<p>The Dbml.cst template is used to create a LINQ to SQL dbml file.&nbsp; The file 
conforms to the Microsoft DbmlSchema.xsd schema.&nbsp; This is the same document 
that the LINQ to SQL designer uses.&nbsp; The generated dbml file from this template 
can also be edited from the LINQ to SQL designer.&nbsp; </p>
<p>The template will create a new file if it doesn&#39;t exist.&nbsp; If the file does 
exist, the template will read it in and update it.&nbsp; This allows you to make 
changes to the file and not have it overwrite if the template is re-ran.&nbsp; However, 
only some of the attributes are safe from overwriting.&nbsp; Here is a list of safe 
attributes.&nbsp; They will be listed as an xpath.</p>
<p>Safe Attributes to change in the Dbml file ...</p>
<ul>
	<li>Database/@Class - The name of the DataContext class that will be generated.</li>
	<li>Database/@EntityNamespace - The namespace for the entity classes.</li>
	<li>Database/@ContextNamespace - The namespace for the DataContext class.</li>
	<li>Table/@Member - The property name for the table in the DataContext class.</li>
	<li>Type/@Name - The name of the entity class.</li>
	<li>Column/@Member - The property name for the column in the entity class.</li>
	<li>Column/@Storage - The private field LINQ to SQL will us to assign values 
	to.</li>
	<li>Association/@Member - The property name for this association.</li>
	<li>Association/@Storage - The private field LINQ to SQL will us to assign values 
	the association to.</li>
	<li>Function/@Method - The name of the method for the database procedure.</li>
	<li>Parameter/@Parameter - The method argument name that maps to the database 
	procedure parameter.</li>
</ul>
<p><b>Warning:</b> Be aware that the template will drop tables, columns and associations 
that it did not find in the database.</p>
<p>Properties on the Dbml.cst template:</p>
<table cellpadding="4">
	<tr>
		<th>Property</th>
		<th>Description</th>
	</tr>
	<tr>
		<td>CleanExpression</td>
		<td>List of regular expressions to clean table, view, column and procedure 
		names.&nbsp; Any matched text found will be removed from the name.</td>
	</tr>
	<tr>
		<td>IgnoreList</td>
		<td>List of regular expressions used to ignore tables, views and procedures 
		when generating mapping.</td>
	</tr>
	<tr>
		<td>IncludeFunctions</td>
		<td>Include stored procedures and user functions in mapping.</td>
	</tr>
	<tr>
		<td>IncludeViews</td>
		<td>Include views in mapping.</td>
	</tr>
	<tr>
		<td>SourceDatabase</td>
		<td>The source database to generate the dbml file for.</td>
	</tr>
	<tr>
		<td>ContextNamespace</td>
		<td>The namespace to use for the context class file.</td>
	</tr>
	<tr>
		<td>EntityNamespace</td>
		<td>The namespace to use for the entity class files.</td>
	</tr>
	<tr>
		<td>DbmlFile</td>
		<td>The path to the dbml file to generate.</td>
	</tr>
</table>
<h2><a title="Entities.cst" name="Entities.cst">Entities.cst Template</a></h2>
<p>The entities template generates the entity classes needed by LINQ.&nbsp; The 
classes are generated from a dbml file.&nbsp; You can modify the names for classes 
and properties by editing the dbml file.&nbsp; See
<a href="#Dbml.cst">Dbml.cst</a> for a list of safe attributes 
to change in the dbml file.</p>
<p>The template will generate 2 files for every Type in the dbml file.&nbsp; One 
    file will be the generated partial class that can not be changed as it is 
    overwritten when the template is re-ran.&nbsp; It will have the following file 
    name... &lt;entity&gt;.Generated.vb</p>
<p>The second file is a partial class that can be modified as it will not be 
    re-generated.&nbsp; You can implement the partial methods in this file.&nbsp; 
    Some partial method stubs are created by default. This file will be named... 
    &lt;entity&gt;.vb</p>
<p>If you set the project file property on the template, the generated files will 
be added to the project.&nbsp; The file that can not be modified will be hidden 
under the file that can be changed.</p>
<p>Properties on the Entities.cst template:</p>
<table cellpadding="4">
	<tr>
		<th>Property</th>
		<th>Description</th>
	</tr>
	<tr>
		<td>DbmlFile</td>
		<td>The path to the dbml file used generate the entities from.</td>
	</tr>
	<tr>
		<td>OutputDirectory</td>
		<td>The folder to save the generated files.</td>
	</tr>
	<tr>
		<td>ProjectFile</td>
		<td>The Visual Studio project file to add the generated files to.</td>
	</tr>
</table>
<h2><a title="Managers.cst" name="Managers.cst">Managers.cst Template</a></h2>
<p>The manager template is for helping you get started with business logic for the 
LINQ entities.&nbsp; The managers will have common queries that are created from 
keys and indexes on the table.&nbsp; The manager will also have rules for the entity 
properties to make sure required fields are not null and that the length of a string 
does not exceed the max length the column allows.</p>
<p>The template works by creating a second partial class that has a Manager 
property.&nbsp; The manager will then have a property for each entity that has a 
manager. Here is a sample of the syntax for using the managers:</p>
<p><code><span style="color: #0000FF">Dim</span> db <span style="color: #0000FF">As New </span>PetshopDataContext(
    <span>Enter Connection info here</span>)
</code></p>
<p><code style="color: #008000"> ' use the primary key
</code></p>
<p><code> <span style="color: #0000FF">Dim</span> prod <span style="color: #0000FF">As</span> Product = db.Manager.Product.GetByKey(1)
</code></p>
<p><code style="color: #008000"> 'By CategoryID
</code></p>
<p><code><span style="color: #0000FF">Dim</span> bugs <span style="color: #0000FF">As</span> IQueryable = db.Manager.Product.GetByCategoryId(<span 
        style="color: #FF0000">"Bugs"</span>)
</code></p>
<p><code style="color: #008000"> ' the methods return IQueryable so you can add expressions
</code></p>
<p><code><span style="color: #0000FF">Dim</span> bugs <span style="color: #0000FF">As</span> IOrderedQueryable = db.Manager.Product.GetByCategoryId("Bugs").OrderBy(<span style="color: #0000FF">Function</span>(prod <span style="color: #0000FF">As</span> Product) prod.Name)
</code></p>
<p>The manager also provides a business rules engine to your entities. In addition 
to the default validation rules that are generated, you can add custom rules by 
implementing the AddRules partial method in the custom entity class.</p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
	<p style="margin: 0px;"><span style="color: blue;">Shared Partial Sub </span>
	AddRules()</p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">' Rule 
	allows the Name property to be a max of 150 characters.</span></p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; RuleManager.AddShared(<span style="color: blue;">Of</span> Task)(<span style="color: blue;">New</span> 
	LengthRule(<span style="color: #a31515;">&quot;Name&quot;</span>, 150))</p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">' Rule 
	that validates the value of the property using regex.</span></p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; RuleManager.AddShared(<span style="color: blue;">Of</span> Task)(<span style="color: blue;">New</span> 
	RegexRule(<span style="color: #a31515;">&quot;Name&quot;</span>,
	<span style="color: #a31515;">&quot;.*&quot;</span>))</p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">' Rule 
	allows only users in certain security roles to update.</span></p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; RuleManager.AddShared(<span style="color: blue;">Of</span> Task)(<span style="color: blue;">New</span> 
	UpdateRule(
	<span style="margin: 0px;">
	<span style="color: blue;">New</span> <span style="color: blue;">String</span>() = 
	{ <span style="color: #a31515;">&quot;Administrator&quot;</span>,
	<span style="color: #a31515;">&quot;Updaters&quot;</span> }))</p>
	<p style="color: blue; margin: 0px;">End Sub</p>
</div>
<p>Properties on the Managers.cst template:</p>
<table cellpadding="4">
	<tr>
		<th>Property</th>
		<th>Description</th>
	</tr>
	<tr>
		<td>SourceDatabase</td>
		<td>The source database to keys and indexes from for generating the manager 
		classes.</td>
	</tr>
	<tr>
		<td>DbmlFile</td>
		<td>The path to the dbml file used generate the manager classes from.</td>
	</tr>
	<tr>
		<td>ManagerContextName</td>
		<td>The class name of the DataContext that supports the managers.</td>
	</tr>
	<tr>
		<td>ManagerDirectory</td>
		<td>The folder to save the generated manager files.</td>
	</tr>
	<tr>
		<td>ManagerNamespace</td>
		<td>The namespace to use for the generated manager class files.</td>
	</tr>
	<tr>
		<td>ProjectFile</td>
		<td>The Visual Studio project file to add the generated files to.</td>
	</tr>
</table>
<h2><a title="Managers.cst" name="Managers.cst">Queries.cst Template</a></h2>
<p>The queries template is an optional alternative to the manager template.&nbsp; 
If you don&#39;t want to use the manager framework, you can use this template to 
generate some common queries for an entity.&nbsp; While its possible to use both 
the manger and query templates, they do duplicate some functionality. The 
template works by generating an extension module for Table&lt;Entity&gt;.&nbsp; This 
allows the queries to be off the DataContext. Where as Manager uses a partial class 
    to hide the generated code. Queries.cst uses CodeSmiths preserved merge 
    capability.</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;}??\fs20 \cf1 SampleDataContext\cf0  db = \cf4 new\cf0  \cf1 SampleDataContext\cf0 ();\par ??\cf1 Task\cf0  task = db.Task.GetByID(1);\par ??}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
	<p style="margin: 0px;"><p><code><span style="color: #0000FF">Dim</span> db <span style="color: #0000FF">As New </span>PetshopDataContext(
    <span>Enter Connection info here</span>)
	<p style="margin: 0px;"><span style="color: blue;">Dim</span> prod <span style="color: blue;">As</span> Product = db.Product.GetByKey(1) </p>
</div>
<!--EndFragment-->
<p>This is an example of what the extension class looks like.</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red128\green128\blue128;\red255\green255\blue255;\red0\green128\blue0;\red0\green0\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 ///\cf3  \cf1 &lt;summary&gt;\par ??///\cf3  The query extension class for Task.\par ??\cf1 ///\cf3  \cf1 &lt;/summary&gt;\par ??\cf4 public\cf0  \cf4 static\cf0  \cf4 partial\cf0  \cf4 class\cf0  \cf6 TaskQueryExtension\par ??\cf0 \{\par ??    \par ??    \par ??\par ??    \cf1 ///\cf3  \cf1 &lt;summary&gt;\par ??\cf0     \cf1 ///\cf3  Gets an instance by the primary key.\par ??\cf0     \cf1 ///\cf3  \cf1 &lt;/summary&gt;\par ??\cf0     \cf4 public\cf0  \cf4 static\cf0  \cf6 Task\cf0  GetByTaskID(\cf4 this\cf0  \cf6 Table\cf0 &lt;\cf6 Task\cf0 &gt; entity, \cf4 int\cf0  taskID)\par ??    \{\par ??        \cf4 if\cf0  (entity.Context.LoadOptions == \cf4 null\cf0 ) \par ??            \cf4 return\cf0  \cf6 Query\cf0 .GetByTaskID.Invoke((\cf6 SampleDataContext\cf0 )entity.Context, taskID);\par ??        \cf4 else\par ??\cf0             \cf4 return\cf0  entity.FirstOrDefault(t =&gt; t.TaskID == taskID);\par ??    \}\par ??\par ??    \cf1 ///\cf3  \cf1 &lt;summary&gt;\par ??\cf0     \cf1 ///\cf3  Gets a query by an index.\par ??\cf0     \cf1 ///\cf3  \cf1 &lt;/summary&gt;\par ??\cf0     \cf4 public\cf0  \cf4 static\cf0  \cf6 IQueryable\cf0 &lt;\cf6 Task\cf0 &gt; GetByStatusID(\cf4 this\cf0  \cf6 Table\cf0 &lt;\cf6 Task\cf0 &gt; entity, \cf4 int\cf0  statusID)\par ??    \{\par ??        \cf4 if\cf0  (entity.Context.LoadOptions == \cf4 null\cf0 ) \par ??            \cf4 return\cf0  \cf6 Query\cf0 .GetByStatusID.Invoke((\cf6 SampleDataContext\cf0 )entity.Context, statusID);\par ??        \cf4 else\par ??\cf0             \cf4 return\cf0  entity.Where(t =&gt; t.StatusID == statusID);\par ??    \}\par ??\par ??    \cf1 ///\cf3  \cf1 &lt;summary&gt;\par ??\cf0     \cf1 ///\cf3  Gets a query by an index.\par ??\cf0     \cf1 ///\cf3  \cf1 &lt;/summary&gt;\par ??\cf0     \cf4 public\cf0  \cf4 static\cf0  \cf6 IQueryable\cf0 &lt;\cf6 Task\cf0 &gt; GetByPriorityID(\cf4 this\cf0  \cf6 Table\cf0 &lt;\cf6 Task\cf0 &gt; entity, \cf4 int\cf0  priorityID)\par ??    \{\par ??        \cf4 if\cf0  (entity.Context.LoadOptions == \cf4 null\cf0 ) \par ??            \cf4 return\cf0  \cf6 Query\cf0 .GetByPriorityID.Invoke((\cf6 SampleDataContext\cf0 )entity.Context, priorityID);\par ??        \cf4 else\par ??\cf0             \cf4 return\cf0  entity.Where(t =&gt; t.PriorityID == priorityID);\par ??    \}\par ??\par ??    \cf1 ///\cf3  \cf1 &lt;summary&gt;\par ??\cf0     \cf1 ///\cf3  Gets a query by an index.\par ??\cf0     \cf1 ///\cf3  \cf1 &lt;/summary&gt;\par ??\cf0     \cf4 public\cf0  \cf4 static\cf0  \cf6 IQueryable\cf0 &lt;\cf6 Task\cf0 &gt; GetByCreatedID(\cf4 this\cf0  \cf6 Table\cf0 &lt;\cf6 Task\cf0 &gt; entity, \cf4 int\cf0  createdID)\par ??    \{\par ??        \cf4 if\cf0  (entity.Context.LoadOptions == \cf4 null\cf0 ) \par ??            \cf4 return\cf0  \cf6 Query\cf0 .GetByCreatedID.Invoke((\cf6 SampleDataContext\cf0 )entity.Context, createdID);\par ??        \cf4 else\par ??\cf0             \cf4 return\cf0  entity.Where(t =&gt; t.CreatedID == createdID);\par ??    \}\par ??\par ??    \cf1 ///\cf3  \cf1 &lt;summary&gt;\par ??\cf0     \cf1 ///\cf3  Gets a query by an index.\par ??\cf0     \cf1 ///\cf3  \cf1 &lt;/summary&gt;\par ??\cf0     \cf4 public\cf0  \cf4 static\cf0  \cf6 IQueryable\cf0 &lt;\cf6 Task\cf0 &gt; GetByAssignedID(\cf4 this\cf0  \cf6 Table\cf0 &lt;\cf6 Task\cf0 &gt; entity, \cf4 int\cf0  assignedID)\par ??    \{\par ??        \cf4 if\cf0  (entity.Context.LoadOptions == \cf4 null\cf0 ) \par ??            \cf4 return\cf0  \cf6 Query\cf0 .GetByAssignedID.Invoke((\cf6 SampleDataContext\cf0 )entity.Context, assignedID);\par ??        \cf4 else\par ??\cf0             \cf4 return\cf0  entity.Where(t =&gt; t.AssignedID == assignedID);\par ??    \}\par ??\par ??\cf4     #region\cf0  Query\par ??    \cf1 ///\cf3  \cf1 &lt;summary&gt;\par ??\cf0     \cf1 ///\cf3  A private class for lazy loading static compiled queries.\par ??\cf0     \cf1 ///\cf3  \cf1 &lt;/summary&gt;\par ??\cf0     \cf4 private\cf0  \cf4 static\cf0  \cf4 partial\cf0  \cf4 class\cf0  \cf6 Query\par ??\cf0     \{\par ??\par ??        \cf4 internal\cf0  \cf4 static\cf0  \cf4 readonly\cf0  \cf6 Func\cf0 &lt;\cf6 SampleDataContext\cf0 , \cf4 int\cf0 , \cf6 Task\cf0 &gt; GetByTaskID = \par ??            \cf6 CompiledQuery\cf0 .Compile(\par ??                (\cf6 SampleDataContext\cf0  db, \cf4 int\cf0  taskID) =&gt; \par ??                    db.Task.FirstOrDefault(t =&gt; t.TaskID == taskID));\par ??\par ??        \cf4 internal\cf0  \cf4 static\cf0  \cf4 readonly\cf0  \cf6 Func\cf0 &lt;\cf6 SampleDataContext\cf0 , \cf4 int\cf0 , \cf6 IQueryable\cf0 &lt;\cf6 Task\cf0 &gt;&gt; GetByStatusID = \par ??            \cf6 CompiledQuery\cf0 .Compile(\par ??                (\cf6 SampleDataContext\cf0  db, \cf4 int\cf0  statusID) =&gt; \par ??                    db.Task.Where(t =&gt; t.StatusID == statusID));\par ??\par ??        \cf4 internal\cf0  \cf4 static\cf0  \cf4 readonly\cf0  \cf6 Func\cf0 &lt;\cf6 SampleDataContext\cf0 , \cf4 int\cf0 , \cf6 IQueryable\cf0 &lt;\cf6 Task\cf0 &gt;&gt; GetByPriorityID = \par ??            \cf6 CompiledQuery\cf0 .Compile(\par ??                (\cf6 SampleDataContext\cf0  db, \cf4 int\cf0  priorityID) =&gt; \par ??                    db.Task.Where(t =&gt; t.PriorityID == priorityID));\par ??\par ??        \cf4 internal\cf0  \cf4 static\cf0  \cf4 readonly\cf0  \cf6 Func\cf0 &lt;\cf6 SampleDataContext\cf0 , \cf4 int\cf0 , \cf6 IQueryable\cf0 &lt;\cf6 Task\cf0 &gt;&gt; GetByCreatedID = \par ??            \cf6 CompiledQuery\cf0 .Compile(\par ??                (\cf6 SampleDataContext\cf0  db, \cf4 int\cf0  createdID) =&gt; \par ??                    db.Task.Where(t =&gt; t.CreatedID == createdID));\par ??\par ??        \cf4 internal\cf0  \cf4 static\cf0  \cf4 readonly\cf0  \cf6 Func\cf0 &lt;\cf6 SampleDataContext\cf0 , \cf4 int\cf0 , \cf6 IQueryable\cf0 &lt;\cf6 Task\cf0 &gt;&gt; GetByAssignedID = \par ??            \cf6 CompiledQuery\cf0 .Compile(\par ??                (\cf6 SampleDataContext\cf0  db, \cf4 int\cf0  assignedID) =&gt; \par ??                    db.Task.Where(t =&gt; t.AssignedID == assignedID));\par ??\par ??    \}\par ??\cf4     #endregion\par ??\cf0 \}\par ??}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
	<p style="margin: 0px;"><span style="color: gray;">///</span><span style="color: green;">
	</span><span style="color: gray;">&lt;summary&gt;</span></p>
	<p style="margin: 0px;"><span style="color: gray;">///</span><span style="color: green;"> 
	The query extension class for Task.</span></p>
	<p style="margin: 0px;"><span style="color: gray;">///</span><span style="color: green;">
	</span><span style="color: gray;">&lt;/summary&gt;</span></p>
	<p style="margin: 0px;"><span style="color: blue;">Public</span>
	<span style="color: blue;">Module</span> ProductQueryExtension</p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">'''</span><span style="color: green;">
	</span><span style="color: gray;">&lt;summary&gt;</span></p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">'''</span><span style="color: green;"> 
	Gets an instance by the primary key.</span></p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">'''</span><span style="color: green;">
	</span><span style="color: gray;">&lt;/summary&gt;</span></p>
	<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; 
    &lt;Extension()&gt; _ </p>
    <p style="margin: 0px 0px 0px 40px; color: #000000;"><span style="color: #0000FF">Public Function</span> GetByKey(entity 
        <span style="color: #0000FF">As</span> Table(<span style="color: #0000FF">Of </span>
        Product), productId <span style="color: #0000FF">As String </span>)<span 
            style="color: #0000FF"> As</span> Product </p>
    <p style="margin: 0px 0px 0px 80px;"><span style="color: #0000FF">If</span> entity.Context.LoadOptions 
        <span style="color: #0000FF">Is Nothing Then</span> </p>
    <p style="margin: 0px 0px 0px 120px;"><span style="color: #0000FF">Return</span>
        Query.GetByKey.Invoke(<span style="color: #0000FF">DirectCast</span>(entity.Context,PetshopDataContext), productId) </p>
    <p style="margin: 0px 0px 0px 80px; color:Blue">Else </p>
    <p style="margin: 0px 0px 0px 120px;"><span style="color: #0000FF">Return </span>
        entity.FirstOrDefault(<span style="color: #0000FF">Function</span>(p)p.ProductId = productId) </p>
    <p style="margin: 0px 0px 0px 80px; color:Blue">End If </p>
    <p style="margin: 0px 0px 0px 40px; color:Blue">End Function </p>
    <p style="margin: 0px 0px 0px 40px; color: gray;">&#39;&#39;&#39; &lt;summary&gt;</p>
    <p style="margin: 0px 0px 0px 40px; color:Green">&#39;&#39;&#39; Gets a query by an index. </p>
    <p style="margin: 0px 0px 0px 40px; color:Gray">&#39;&#39;&#39; &lt;summary&gt;</p>
    <code></code><p style="margin: 0px 0px 0px 40px;">&nbsp;&lt;Extension()&gt; _ </p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: #0000FF">Public Function</span> GetByName(entity 
        <span style="color: #0000FF">As</span> Table(<span>Of</span> Product), [Name] 
        <span style="color: #0000FF">As 
        String</span>) <span style="color: #0000FF">As</span> IQueryable(<span 
            style="color: #0000FF">Of</span> Product) </p>
    <p style="margin: 0px 0px 0px 80px;"><span style="color: #0000FF">If</span> entity.Context.LoadOptions 
        <span style="color: #0000FF">Is Nothing Then </span>
        </p>
    <p style="margin: 0px 0px 0px 120px;"><span style="color: #0000FF">Return</span> Query.GetByName.Invoke(<span 
            style="color: #0000FF">DirectCast</span>(entity.Context,PetshopDataContext), 
        [Name]) </p>
    <p style="margin: 0px 0px 0px 80px; color:Blue">Else </p>
    <p style="margin: 0px 0px 0px 120px;"><span style="color: #0000FF">Return</span> entity.Where(<span 
            style="color: #0000FF">Function</span>(p)p.Name = [Name]) </p>
    <p style="margin: 0px 0px 0px 80px; color:Blue">End If </p>
    <p style="margin: 0px 0px 0px 40px; color:Blue">End Function</p></code>
	<p style="margin: 0px;">&nbsp;</p>
	<p style="margin: 0px;"><span style="color: blue;">&nbsp;&nbsp;&nbsp; 
	</span></p>
	<p style="margin: 0px;"><span style="color: green;">&nbsp;&nbsp;&nbsp;&nbsp; &#39;Insert 
        User Defined Extensions here. </span></p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: green;">&#39;Anything outside of this Region will be lost at 
        regeneration </span></p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: blue;">#Region</span><span> &quot;User Extensions&quot; </span></p>
    <p style="margin: 0px 0px 0px 40px;">&nbsp;</p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: blue;">#End Region </span></p>
    <p style="margin: 0px 0px 0px 40px;">&nbsp;</p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: blue;">#region</span><span> &quot;Query&quot; </span></p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: gray;">&#39;&#39;&#39; &lt;summary&gt;</span></p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: green;">&#39;&#39;&#39; A private class for lazy loading static compiled queries. </span></p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: gray;">&#39;&#39;&#39; &lt;summary&gt;
        </span></p>
    <p style="margin: 0px 0px 0px 40px;"><span style="color: blue;">Private Partial Class</span> Query </p>
    <p style="margin: 0px 0px 0px 80px;"><span style="color: blue;">Friend Shared ReadOnly</span> GetByKey <span style="color: blue;">As</span> Func(<span style="color: blue;">Of</span> 
        PetshopDataContext, <span style="color: blue;">String</span>, Product) = _</span></p>
    <p style="margin: 0px 0px 0px 120px;"><span style="color: blue;">CompiledQuery.Compile( _</span></p>
    <p style="margin: 0px 0px 0px 160px;"><span style="color: blue;">Function</span>(db <span style="color: blue;">As </span>
        PetshopDataContext , productId <span style="color: blue;">As String</span>) _</p>
    <p style="margin: 0px 0px 0px 160px;">db.Product.FirstOrDefault(<span style="color: blue;">Function</span>(p)p.ProductId = productId)) </span></p>
    <p style="margin: 0px 0px 0px 80px;">&nbsp;</p>
    <p style="margin: 0px 0px 0px 80px;"><span style="color: blue;">Friend Shared
        ReadOnly</span> GetByName <span style="color: blue;">As</span> Func(<span style="color: blue;">Of</span> PetshopDataContext, <span style="color: blue;">String</span>, IQueryable(<span style="color: blue;">Of</span> 
        Product)) = _</p>
    <p style="margin: 0px 0px 0px 80px;">&nbsp;&nbsp;&nbsp; CompiledQuery.Compile( _ </p>
    <p style="margin: 0px 0px 0px 160px;"><span style="color: blue;">Function</span>(db <span style="color: blue;">As</span> PetshopDataContext , 
        [Name]<span style="color: blue;"> As String</span>) _</p>
    <p style="margin: 0px 0px 0px 160px;">db.Product.Where(<span style="color: blue;">Function</span>(p)p.Name = [Name]))</span></p>
	<p style="margin: 0px;">&nbsp;</p>
    <p style="margin: 0px 0px 0px 80px; color: #008000">&#39; Add your compiled queries here. </p>
    <p style="margin: 0px 0px 0px 80px; color: #008000">&#39;Anything outside of this Region will be lost at regeneration </p>
    <p style="margin: 0px 0px 0px 80px;"><span style="color: #0000FF">#Region</span><span style="color:Red"> &quot;User Queries&quot;</span> </p>
    <p style="margin: 0px 0px 0px 80px;">&nbsp;</p>
    <p style="margin: 0px 0px 0px 80px; color:Blue">#End Region </p>
    <p style="margin: 0px 0px 0px 40px; color:Blue">End Class </p>
    <p style="margin: 0px 0px 0px 40px; color:Blue">#End Region </p>
    <p style="color:Blue">End Module</p>
</div>
<!--EndFragment-->
<p>Properties on the Queries.cst template:</p>
<table cellpadding="4">
	<tr>
		<th>Property</th>
		<th>Description</th>
	</tr>
	<tr>
		<td>SourceDatabase</td>
		<td>The source database to keys and indexes from for generating the manager 
		classes.</td>
	</tr>
	<tr>
		<td>DbmlFile</td>
		<td>The path to the dbml file used generate the query classes from.</td>
	</tr>
	<tr>
		<td>QueryDirectory</td>
		<td>The folder to save the generated query extension files.</td>
	</tr>
	<tr>
		<td>ProjectFile</td>
		<td>The Visual Studio project file to add the generated files to.</td>
	</tr>
</table>
<h2><a title="KnownIssues" name="KnownIssues"></a>Known Issues</h2>
<ul>
	<li>The generated DataContext does not set the connection string like the LINQ 
	to SQL designer</li>
</ul>
<h2><a title="Roadmap" name="Roadmap"></a>Roadmap</h2>
<ul>
	<li>Improve manager template</li>
	<li>Unit test generation</li>
	<li>Web service generation</li>
	<li>ASP.NET, Winforms and WPF UI generation</li>
	<li>Visual Basic support</li>
</ul>
<h2><a name="Download"></a>Download</h2>
<p>Download the latest release from CodePlex.</p>
<ul>
	<li>
	<a href="http://www.codeplex.com/codesmith/Release/ProjectReleases.aspx">
	http://www.codeplex.com/codesmith/Release/ProjectReleases.aspx</a></li>
</ul>
<h2>History</h2>
<ul>
	<li>Release 1.3<ul>
		<li>added GetByKey(IEntityKey) common method for manager template</li>
		<li>made managers inherit from base classes in manager template</li>
		<li class="style1">made primary key query use common name GetByKey in 
		manager and query templates</li>
		<li>fix namespace issues when context and entity namespaces aren&#39;t the 
		same</li>
		<li>fix QueryExtension with wrong context name</li>
		<li>fix CompiledQuery issue when there are more then four columns</li>
		<li>fix issue with compiled queries and load options</li>
		<li>fix bugs when dbml file is loaded in designer </li>
		<li>add support for adding the ConnectionString to the settings file
		</li>
		<li>added Queries.cst to support query extension Methods </li>
		<li class="style1">&nbsp;removed manager data context, now another 
		partial of the DataContext</li>
		<li>added support for unique indexes</li>
		<li>added range rule for dates</li>
		<li>update to .net 3.5 RTM</li>
	</ul>
	</li>
	<li>Release 1.2<ul>
		<li>Fix LinqDataSource issue by making all properties writable</li>
		<li>Added CompiledQuery support to the manager classes</li>
		<li>Added support for overriding the column data type </li>
	</ul>
	</li>
	<li>Release 1.1<ul>
		<li>Changed to use dbml schema as meta data</li>
		<li>Split templates up to allow more flexibility</li>
		<li>Added function, table inheritance, ignoring, and name cleaning 
		support</li>
	</ul>
	</li>
</ul>

<p>&nbsp;</p>

<p class="style1">*Breaking change</p>

</body>

</html>
